﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using MaxStudio.Concrete.DBUtility;

namespace MaxStudio.Concrete.Server.Dao
{
    public partial class ConcreteDaoDataContext : System.Data.Linq.DataContext
    {
        #region SalesOrder

        public List<tb_SalesOrder> GetAllSalesOrder()
        {

            var query = (from obj in this.tb_SalesOrders
                         where obj.IsDelete == 0
                         select obj).ToList();
            return query;
        }
        public tb_SalesOrder GetSalesOrderByContranctSN(string strContranctSN)
        {
            return this.tb_SalesOrders.SingleOrDefault(tb_SalesOrders => tb_SalesOrders.ContranctSN == strContranctSN);
        }

        public tb_SalesOrder GetSalesOrderById(int id)
        {
            return this.tb_SalesOrders.SingleOrDefault(tb_SalesOrders => tb_SalesOrders.Id == id);
        }

        public void CreateSalesOrder(tb_SalesOrder entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.CreateBy = 1;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            tb_SalesOrders.InsertOnSubmit(entity);
            this.SubmitChanges();

        }

        public void ModifySalesOrder(tb_SalesOrder entity)
        {
            tb_SalesOrder instance = this.tb_SalesOrders.SingleOrDefault(p => p.Id == entity.Id);
            instance.ContranctSN = entity.ContranctSN;
            instance.CustomerId = entity.CustomerId;
            instance.Description = entity.Description;
            instance.FullPrice = entity.FullPrice;
            instance.Id = entity.Id;
            instance.Provision = entity.Provision;
            instance.SignDate = entity.SignDate;
            instance.Status = entity.Status;
            instance.Traker = entity.Traker;
            
            instance.PumpingPrice = entity.PumpingPrice;
            instance.OutKMPrice = entity.OutKMPrice;
            instance.OtherPrice = entity.OtherPrice;
            instance.ConcretePrice = entity.ConcretePrice; 
            instance.TotalPrice = entity.TotalPrice;

            instance.IsDelete = entity.IsDelete;
            instance.ModifyBy = 1; //Need to get current user
            instance.ModifyDate = DateTime.Now;
  

            this.SubmitChanges();
        }
        public void DeleteSalesOrder(tb_SalesOrder entity)
        {
            tb_SalesOrder deleteEntity = tb_SalesOrders.SingleOrDefault(p => p.Id == entity.Id);
            tb_SalesOrders.DeleteOnSubmit(deleteEntity);

            this.SubmitChanges();
        }

          #endregion

        #region SalesOrder Prodcut Mapping
        public void CreateOrderProductMapping(tb_Mapping_OrderProduct mappingEntity)
        {
            tb_Mapping_OrderProducts.InsertOnSubmit(mappingEntity);
            this.SubmitChanges();
        }

        public void DeleteOrderProductMapping(tb_Mapping_OrderProduct mappingEntity)
        {
            tb_Mapping_OrderProduct deleteEntity = tb_Mapping_OrderProducts.SingleOrDefault(p => p.Id == mappingEntity.Id);
            tb_Mapping_OrderProducts.DeleteOnSubmit(deleteEntity);

            this.SubmitChanges(); 
        }


        public List<tb_Mapping_OrderProduct> GetMappingOrderProductByOrderId(int orderId)
        {

            var query = (from obj in this.tb_Mapping_OrderProducts
                         where obj.SalesOrderId == orderId
                         select obj).ToList();
            return query;
        }

        #endregion

    }
}
